package com.iOS.controller;

import com.iOS.dto.BlogInfoExecution;
import com.iOS.dto.UserInfoExecution;
import com.iOS.entity.BlogInfo;
import com.iOS.enums.BlogInfoEnum;
import com.iOS.service.IBlogInfoService;
import com.iOS.service.IUserInfoService;
import com.iOS.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/blog")
public class BlogInfoController {
    @Autowired
    private IBlogInfoService blogInfoService;
    @Autowired
    private IUserInfoService userInfoService;

    @RequestMapping(value = "/addition", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> Addition(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String blogTitle= HttpServletRequestUtil.getString(request,"blogTitle");
        String blogContext=HttpServletRequestUtil.getString(request,"blogContext");
        String token = HttpServletRequestUtil.getString(request, "token");
         /*获取图片*/
        MultipartResolver MultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartHttpServletRequest =MultipartResolver.resolveMultipart(request);
        MultipartFile img = multipartHttpServletRequest.getFile("blogPictures");
        if (token==null){
            modelMap.put("success",false);
        }else {
            try {
                UserInfoExecution user = userInfoService.GetUserInfo(token);
                BlogInfo blogInfo=new BlogInfo();
                int userId=user.getUserInfo().getId();
                blogInfo.setUserId(userId);
                blogInfo.setBlogTitle(blogTitle);
                blogInfo.setBlogContext(blogContext);
                blogInfo.setBlogPictures(img.getOriginalFilename());
                BlogInfoExecution addition=blogInfoService.Add(blogInfo,img.getInputStream(),img.getOriginalFilename());
                modelMap.put("success", addition.getState());
                if(blogInfo.getBlogTitle().equals(null)||blogInfo.getBlogContext().equals(null)){
                    modelMap.put("success",false);
                    modelMap.put("errMsg","请输入完整参数");
                }
               if (addition.getState()== BlogInfoEnum.SUCCESS.getState()){
                    modelMap.put("success", true);
                    modelMap.put("userInfo", addition.getBlogInfo());
                }
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        }
        return modelMap;
    }

    @RequestMapping(value = "/{Id}alter", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> Alter(@PathVariable("Id") int id,HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String blogTitle= HttpServletRequestUtil.getString(request,"blogTitle");
        String blogContext=HttpServletRequestUtil.getString(request,"blogContext");
        MultipartResolver MultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartHttpServletRequest =MultipartResolver.resolveMultipart(request);
        MultipartFile img = multipartHttpServletRequest.getFile("blogPictures");
        try {
            BlogInfo blogInfo = new BlogInfo();
            blogInfo.setBlogTitle(blogTitle);
            blogInfo.setBlogContext(blogContext);
            blogInfo.setBlogPictures(img.getOriginalFilename());
            BlogInfoExecution alter = blogInfoService.Alter(blogInfo,img.getInputStream(),img.getOriginalFilename());
            if(blogInfo.getBlogTitle().equals(null)||blogInfo.getBlogContext().equals(null)){
                modelMap.put("success",false);
                modelMap.put("errMsg","请输入完整信息");
            }
            if (alter.getState()== BlogInfoEnum.SUCCESS.getState()){
                modelMap.put("success", true);
                modelMap.put("userInfo",alter.getBlogInfo());
            }
        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
        }
        return modelMap;
    }




    @RequestMapping(value = "/{Id}/delete",method = {RequestMethod.POST})
    @ResponseBody
    public  Map<String, Object> delete(@PathVariable("Id") int id, Model model){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        blogInfoService.Delete(id);
        List<BlogInfo> list = blogInfoService.getBlogList();
        model.addAttribute("list",list);
        return modelMap;
    }




    @RequestMapping(value = "/query",method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> List(Model model) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
       /*获取列表页*/
        List<BlogInfo> list = blogInfoService.getBlogList();
        model.addAttribute("list",list);
        return modelMap;
    }



    @RequestMapping(value = "/{Id}/detail",method = {RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> detail(@PathVariable("Id") int id, Model model){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        BlogInfo blogInfo = blogInfoService.getById(id);
        model.addAttribute("blogInfo",blogInfo);
        return modelMap;
    }




}
